import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;


public class Test {
	
	public static Connection _conn = null;
	public static String PROMPT = "press ENTER to continue...";
	
	public static void main(String[] args) throws Throwable{
//		args = new String[]{"list","widget"};
//		Test.listTCR();
//		System.out.println("Choose a function:");
//		System.out.println("a.Add a widget\tb.Delete a widget\tc.List all widgets");
		_conn = Test.getConnection();
		Test.listTCR("GM_FF3");
		boolean cycle = true;
		System.out.println(cycle);
		
		String choice = Test.showMenu0();
		while(!choice.equals("0")){
			if(choice.equals("1")){
				while(!Test.showMenu1().equals("0")){}
			}
			if(choice.equals("2")){
				while(!Test.showMenu2().equals("0")){}
			}
			if(choice.equals("3")){
				while(!Test.showMenu3().equals("0")){}
			}
			if(choice.equals("4")){
				while(!Test.showMenu4().equals("0")){}
			}
			choice = Test.showMenu0();
//			String choice = Test.getInputMessage("Your choice");
//			System.out.println(choice);
			if(choice.equalsIgnoreCase("0")){
				cycle = false;
				break;
			}
			if(choice.equalsIgnoreCase("11")){
				String para1 = Test.getInputMessage("Widget Name to Add");
				System.out.println(para1);
				Test.addWidget(para1);
			}			
			if(choice.equalsIgnoreCase("12")){
				String para1 = Test.getInputMessage("Widget Name to Delete");
				System.out.println(para1);
				Test.deleteWidget(para1);
			}			
			if(choice.equalsIgnoreCase("13")){
//				String para1 = Test.getInputMessage("Widget Name");
//				System.out.println(para1);
				Test.listWidget();
			}			
			
			if(choice.equalsIgnoreCase("21")){
				String widgetName = Test.getInputMessage("Widget Name");
				String tcID = Test.getInputMessage("TC ID to Add");
//				System.out.println(para1);
//				System.out.println(para2);
				Test.addTC(widgetName, tcID);
			}			

			if(choice.equalsIgnoreCase("22")){
//				String widgetName = Test.getInputMessage("Widget Name");
				String tcID = Test.getInputMessage("TC ID to Delete");
//				System.out.println(para1);
//				System.out.println(para2);
				Test.deleteTC(tcID);
			}			

			if(choice.equalsIgnoreCase("23")){
//				String widgetName = Test.getInputMessage("Widget Name");
//				String tcID = Test.getInputMessage("TC ID to Delete");
//				System.out.println(para1);
//				System.out.println(para2);
				Test.listTC();
			}			

			if(choice.equalsIgnoreCase("31")){
				String taskName = Test.getInputMessage("Task Name to Add");
//				System.out.println(para1);
				Test.addTask(taskName);
			}			

			if(choice.equalsIgnoreCase("32")){
				String taskName = Test.getInputMessage("Task Name to Delete");
//				System.out.println(para1);
				Test.deleteTask(taskName);
			}			

			if(choice.equalsIgnoreCase("33")){
				Test.listTask(true);
			}			

			if(choice.equalsIgnoreCase("41")){
				String runName = Test.getInputMessage("Task Name");
				String tcID = Test.getInputMessage("TC ID");
				String tester = Test.getInputMessage("Tester");
				String result = Test.getInputMessage("Result(PASS/FAIL/NA)");
//				System.out.println(para1);
//				System.out.println(para2);
				Test.addTCR(runName, tcID, tester, result);
			}	
			
			if(choice.equalsIgnoreCase("42")){
				ArrayList<String> list = Test.listTask(false);
				System.out.println("Available task name list:");
				for(int i=0;i<list.size();i++){
					System.out.println((i+1)+"=" + list.get(i).toString());
				}
				String runName = Test.getInputMessage("Task Name");
				String tcID = Test.getInputMessage("TC ID");
//				String tester = Test.getInputMessage("Tester");
//				String result = Test.getInputMessage("Result(PASS/FAIL/NA)");
//				System.out.println(para1);
//				System.out.println(para2);
				Test.deleteTCR(runName, tcID);
			}	
			
			if(choice.equalsIgnoreCase("43")){
//				String widgetName = Test.getInputMessage("Widget Name");
//				String tcID = Test.getInputMessage("TC ID to Delete");
//				System.out.println(para1);
//				System.out.println(para2);
				Test.listTCR();
			}	
			
//			Test.getInputMessage(PROMPT);
			
		}
		try{
			_conn.close();
		}catch(Exception e){
			_conn = null;
		}
		
		if(true){
			return;
		}
		if(args.length==6 && args[0].equalsIgnoreCase("Add")&&args[1].equalsIgnoreCase("TCR")){
			Test.addTCR(args[2], args[3], args[4], args[5]);			
			return;
		}
		
		if(args.length==4 && args[0].equalsIgnoreCase("Add")&&args[1].equalsIgnoreCase("TC")){
			Test.addTC(args[2],args[3]);				
			return;
		}

		if(args.length==4 && args[0].equalsIgnoreCase("Delete")&&args[1].equalsIgnoreCase("TCR")){
			Test.deleteTCR(args[2], args[3]);			
			return;
		}

		if(args.length==3 && args[0].equalsIgnoreCase("Delete")&&args[1].equalsIgnoreCase("TC")){
			Test.deleteTC(args[2]);				
			return;
		}
		
		if(args.length==3){
			if(args[0].equalsIgnoreCase("Add")&&args[1].equalsIgnoreCase("Widget")){
				Test.addWidget(args[2]);				
				return;
			}

			if(args[0].equalsIgnoreCase("Delete")&&args[1].equalsIgnoreCase("Widget")){
				Test.deleteWidget(args[2]);				
				return;
			}
			
//			if(args[0].equalsIgnoreCase("Delete")&&args[1].equalsIgnoreCase("TC")){
//				Test.deleteTC(args[2]);				
//				return;
//			}

		}
		if(args.length==2 & args[0].equalsIgnoreCase("list")&&args[1].equalsIgnoreCase("TCR")){
			Test.listTCR();			
			return;
		}

		if(args.length==2){
			if(args[0].equalsIgnoreCase("list")&&args[1].equalsIgnoreCase("Widget")){
				Test.listWidget();			
				return;
			}
			if(args[0].equalsIgnoreCase("list")&&args[1].equalsIgnoreCase("TC")){
				Test.listTC();
				return;
			}
		}
		System.out.println("Bad command parameter");
	}

	public static void showMenu(){
		System.out.println("\r\nChoose a function:    input 0 to quit");
		System.out.println("11.[Add a widget]\t12.[Delete a widget]\t13.[List all widgets]");
		System.out.println("21.[Add a TC]\t\t22.[Delete a TC]\t23.[List all TCs]");
		System.out.println("31.[Add a Task]\t\t32.[Delete a Task]\t33.[List all Tasks]");
		System.out.println("41.[Add a TCR]\t\t42.[Delete a TCR]\t43.[List all TCRs]");
	}

	public static String showMenu0(){
		System.out.println("\r\nMenu List:");
		System.out.println("0.Quit");
		System.out.println("1.Widget Management");
		System.out.println("2.Test Case Management");
		System.out.println("3.Task Management");
		System.out.println("4.Test Case Run Management");
		String choice = "";
		boolean valid = false;
		while(!valid){
			String list = "01234";
			choice = Test.getInputMessage("Please choose a menu");
			if(list.indexOf(choice)>=0 && choice.length()==1){
				valid = true;
				break;
			}
		}
		return choice;
	}

	public static String showMenu1(){
		System.out.println("\r\nMenu List:");
		System.out.println("0.Main Menu");
		System.out.println("1.Add Widget");
		System.out.println("2.Delete Widget");
		System.out.println("3.View All Widgets");
		String choice = "";
		boolean valid = false;
		while(!valid){
			String list = "0123";
			choice = Test.getInputMessage("Please choose a menu");
			if(list.indexOf(choice)>=0 && choice.length()==1){
				valid = true;
				break;
			}
		}
		if(choice.equalsIgnoreCase("1")){
			String para1 = Test.getInputMessage("Widget Name to Add");
			System.out.println(para1);
			Test.addWidget(para1);
		}			
		if(choice.equalsIgnoreCase("2")){
			String para1 = Test.getInputMessage("Widget Name to Delete");
			System.out.println(para1);
			Test.deleteWidget(para1);
		}			
		if(choice.equalsIgnoreCase("3")){
//				String para1 = Test.getInputMessage("Widget Name");
//				System.out.println(para1);
			Test.listWidget();
		}								

		return choice;
	}

	public static String showMenu2(){
		System.out.println("\r\nMenu List:");
		System.out.println("0.Main Menu");
		System.out.println("1.Add Test Case");
		System.out.println("2.Delete Test Case");
		System.out.println("3.View All Test Cases");
		String choice = "";
		boolean valid = false;
		while(!valid){
			String list = "0123";
			choice = Test.getInputMessage("Please choose a menu");
			if(list.indexOf(choice)>=0 && choice.length()==1){
				valid = true;
				break;
			}
		}
		if(choice.equalsIgnoreCase("1")){
			String widgetName = Test.getInputMessage("Widget Name");
			String tcID = Test.getInputMessage("TC ID to Add");
//			System.out.println(para1);
//			System.out.println(para2);
			Test.addTC(widgetName, tcID);
//			showMenu2();
		}			

		if(choice.equalsIgnoreCase("2")){
//			String widgetName = Test.getInputMessage("Widget Name");
			String tcID = Test.getInputMessage("TC ID to Delete");
//			System.out.println(para1);
//			System.out.println(para2);
			Test.deleteTC(tcID);
//			showMenu2();
		}			

		if(choice.equalsIgnoreCase("3")){
//			String widgetName = Test.getInputMessage("Widget Name");
//			String tcID = Test.getInputMessage("TC ID to Delete");
//			System.out.println(para1);
//			System.out.println(para2);
			Test.listTC();
//			showMenu2();
		}			

		return choice;
	}

	public static String showMenu3(){
		System.out.println("\r\nMenu List:");
		System.out.println("0.Main Menu");
		System.out.println("1.Add Task");
		System.out.println("2.Delete Task");
		System.out.println("3.View All Tasks");
		String choice = "";
		boolean valid = false;
		while(!valid){
			String list = "0123";
			choice = Test.getInputMessage("Please choose a menu");
			if(list.indexOf(choice)>=0 && choice.length()==1){
				valid = true;
				break;
			}
		}
		if(choice.equalsIgnoreCase("1")){
			String taskName = Test.getInputMessage("Task Name to Add");
//			System.out.println(para1);
			Test.addTask(taskName);
		}			

		if(choice.equalsIgnoreCase("2")){
			String taskName = Test.getInputMessage("Task Name to Delete");
//			System.out.println(para1);
			Test.deleteTask(taskName);
		}			

		if(choice.equalsIgnoreCase("3")){
			Test.listTask(true);
		}			

		return choice;
	}

	public static String showMenu4(){
		System.out.println("\r\nMenu List:");
		System.out.println("0.Main Menu");
		System.out.println("1.Add Test Case Run");
		System.out.println("2.Delete Test Case Run");
		System.out.println("3.View All Test Case Run");
		System.out.println("4.Set Default Task");
		System.out.println("5.Set Default Widget");
		
		String choice = "";
		boolean valid = false;
		while(!valid){
			String list = "01234";
			choice = Test.getInputMessage("Please choose a menu");
			if(list.indexOf(choice)>=0 && choice.length()==1){
				valid = true;
				break;
			}
		}
		if(choice.equalsIgnoreCase("1")){
			String runName = Test.getInputMessage("Task Name");
			String tcID = Test.getInputMessage("TC ID");
			String tester = Test.getInputMessage("Tester");
			String result = Test.getInputMessage("Result(PASS/FAIL/NA)");
//			System.out.println(para1);
//			System.out.println(para2);
			Test.addTCR(runName, tcID, tester, result);
		}	
		
		if(choice.equalsIgnoreCase("2")){
			ArrayList<String> list = Test.listTask(false);
			System.out.println("Available task name list:");
			for(int i=0;i<list.size();i++){
				System.out.println((i+1)+"=" + list.get(i).toString());
			}
			String runName = Test.getInputMessage("Task Name");
			String tcID = Test.getInputMessage("TC ID");
//			String tester = Test.getInputMessage("Tester");
//			String result = Test.getInputMessage("Result(PASS/FAIL/NA)");
//			System.out.println(para1);
//			System.out.println(para2);
			Test.deleteTCR(runName, tcID);
		}	
		
		if(choice.equalsIgnoreCase("3")){
//			String widgetName = Test.getInputMessage("Widget Name");
//			String tcID = Test.getInputMessage("TC ID to Delete");
//			System.out.println(para1);
//			System.out.println(para2);
			Test.listTCR();
		}	
		
		if(choice.equalsIgnoreCase("4")){
//			String widgetName = Test.getInputMessage("Widget Name");
//			String tcID = Test.getInputMessage("TC ID to Delete");
//			System.out.println(para1);
//			System.out.println(para2);
			Test.listTask(true);
		}	

		return choice;
	}

	public static Connection getConnection(){
		return connect("9.123.119.250", "50000", "result","administrator","passw0rd");
	}

	public static Connection connect(String serverName,String port,String dbName,String uid, String pwd){
		String dbUrl ="jdbc:db2://" + serverName + ":" + port + "/" + dbName; 
		Connection conn = null;	
		try {
			System.out.print("Try to connect to db " + dbUrl + "...");
			Class.forName("com.ibm.db2.jcc.DB2Driver");
			conn = DriverManager.getConnection(dbUrl,uid,pwd);
			System.out.println("Connected");
			return conn;
		}catch(Exception e){
//			log.error(e.getMessage());
			return null; 
		}
	}

	
	public static void addWidget(String widgetName){
//		Connection conn = getConnection();	
		
		PreparedStatement pstmt = null;
//		int numUpd;
		String sql = "insert into widgetlist values ('" + widgetName + "')";
//		sql += "values(?,?,?,?,?,?,?,?)"; 
		try {
			System.out.println("Try to add widget name " + widgetName);
			pstmt = _conn.prepareStatement( sql );
//			pstmt.setString(1,widgetName);      	//status
//			pstmt.setString(2,build);			//conf_id
//			pstmt.setString(3,tcid);			//tcid
//			pstmt.setString(4,conf);			//conf
			pstmt.execute();
			System.out.println("Succeed");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	public static void addTask(String taskName){
//		Connection conn = getConnection();	
		
		PreparedStatement pstmt = null;
//		int numUpd;
		String sql = "insert into tasklist values ('" + taskName + "')";
//		sql += "values(?,?,?,?,?,?,?,?)"; 
		try {
			System.out.println("Try to add task name " + taskName);
			pstmt = _conn.prepareStatement( sql );
//			pstmt.setString(1,widgetName);      	//status
//			pstmt.setString(2,build);			//conf_id
//			pstmt.setString(3,tcid);			//tcid
//			pstmt.setString(4,conf);			//conf
			pstmt.execute();
			System.out.println("Succeed");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	public static void addTC(String widgetName,String tcID){
//		Connection conn = getConnection();	
		
		PreparedStatement pstmt = null;
//		int numUpd;
		String sql = "insert into tclist(tc_id,widget_name) values ('" + tcID + "','" + widgetName + "')";
//		sql += "values(?,?,?,?,?,?,?,?)"; 
		try {
			System.out.println("Try to add test case " + tcID + " in widget " + widgetName);
			pstmt = _conn.prepareStatement( sql );
//			pstmt.setString(1,widgetName);      	//status
//			pstmt.setString(2,build);			//conf_id
//			pstmt.setString(3,tcid);			//tcid
//			pstmt.setString(4,conf);			//conf
			pstmt.execute();
			System.out.println("Succeed");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	public static void addTCR(String runName, String tcID, String tester, String result){
//		Connection conn = getConnection();	
		
		PreparedStatement pstmt = null;
//		int numUpd;
		String sql = "insert into tcrun(task_name,tc_id,tester,test_result) values (?,?,?,?)";
//		sql += "values(?,?,?,?,?,?,?,?)"; 
		try {
			System.out.println("Try to add test case run...");
			pstmt = _conn.prepareStatement( sql );
			pstmt.setString(1,runName);      	//status
			pstmt.setString(2,tcID);			//conf_id
			pstmt.setString(3,tester);			//tcid
			pstmt.setString(4,result);			//conf
			pstmt.execute();
			System.out.println("Succeed");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	public static void deleteWidget(String widgetName){
//		Connection conn = getConnection();	
		
		PreparedStatement pstmt = null;
//		int numUpd;
		String sql = "delete from widgetlist where widget_name= '" + widgetName + "'";
//		sql += "values(?,?,?,?,?,?,?,?)"; 
		try {
			System.out.println("Try to delete widget name " + widgetName);
			pstmt = _conn.prepareStatement( sql );
//			pstmt.setString(1,widgetName);      	//status
//			pstmt.setString(2,build);			//conf_id
//			pstmt.setString(3,tcid);			//tcid
//			pstmt.setString(4,conf);			//conf
			pstmt.execute();
			System.out.println("Succeed");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	public static void deleteTask(String taskName){
//		Connection conn = getConnection();	
		
		PreparedStatement pstmt = null;
//		int numUpd;
		String sql = "delete from tasklist where task_name= '" + taskName + "'";
//		sql += "values(?,?,?,?,?,?,?,?)"; 
		try {
			System.out.println("Try to delete task name " + taskName);
			pstmt = _conn.prepareStatement( sql );
//			pstmt.setString(1,widgetName);      	//status
//			pstmt.setString(2,build);			//conf_id
//			pstmt.setString(3,tcid);			//tcid
//			pstmt.setString(4,conf);			//conf
			pstmt.execute();
			System.out.println("Succeed");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	public static void deleteTC(String tcid){
//		Connection conn = getConnection();	
		
		PreparedStatement pstmt = null;
//		int numUpd;
		String sql = "delete from tclist where tc_id= '" + tcid + "'";
//		sql += "values(?,?,?,?,?,?,?,?)"; 
		try {
			System.out.println("Try to delete test case " + tcid);
			pstmt = _conn.prepareStatement( sql );
//			pstmt.setString(1,widgetName);      	//status
//			pstmt.setString(2,build);			//conf_id
//			pstmt.setString(3,tcid);			//tcid
//			pstmt.setString(4,conf);			//conf
			pstmt.execute();
			System.out.println("Succeed");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	
	public static void deleteTCR(String runName, String tcid){
//		Connection conn = getConnection();	
		
		PreparedStatement pstmt = null;
//		int numUpd;
		String sql = "delete from tcrun where tc_id= ? and task_name=?";
		
//		sql += "values(?,?,?,?,?,?,?,?)"; 
		try {
			System.out.println("Try to delete test case " + tcid);
			pstmt = _conn.prepareStatement( sql );
			pstmt.setString(1,tcid);      	//status
			pstmt.setString(2,runName);			//conf_id
//			pstmt.setString(3,tcid);			//tcid
//			pstmt.setString(4,conf);			//conf
			pstmt.execute();
			System.out.println("Succeed");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	
	public static void listWidget(){
//		Connection conn = getConnection();	
		PreparedStatement pstmt = null;
		try {
			System.out.println("Try to list widget name ");
			Test.printS();
			String sql = "select widget_name from widgetlist";
			pstmt = _conn.prepareStatement( sql );
			ResultSet list = pstmt.executeQuery();
			int count = 0;
			while(list.next()){
				count++;
				System.out.println(count+"\t" + list.getString("widget_name"));
			}
			Test.printS();
			System.out.println("Succeed..." + count + " rows.");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	
	public static ArrayList<String> listTask(boolean showMsg){
//		Connection conn = getConnection();	
		ArrayList<String> res = new ArrayList<String>();
		PreparedStatement pstmt = null;
		try {
			if(showMsg){
				System.out.println("Try to list task name ");
				Test.printS();				
			}
			String sql = "select task_name from tasklist";
			pstmt = _conn.prepareStatement( sql );
			ResultSet list = pstmt.executeQuery();
			int count = 0;
			while(list.next()){
				res.add(list.getString("task_name"));
				if(showMsg){
					System.out.println(list.getString("task_name"));					
				}
				count++;
			}
			if(showMsg){
				Test.printS();
				System.out.println("Succeed..." + count + " rows.");
			}
			pstmt.close();                    
//			conn.close();
//			return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if(showMsg){
				System.out.println("Fail");
				e.printStackTrace();
			}
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
//				e1.printStackTrace();
			} 
		}
		return res;
	} 

	public static void listTC(){
//		Connection conn = getConnection();	
		PreparedStatement pstmt = null;
		try {
			System.out.println("Try to list test case ");
			Test.printS();
			String sql = "select widget_name,tc_id from tclist";
			pstmt = _conn.prepareStatement( sql );
			ResultSet list = pstmt.executeQuery();
			int count = 0;
			while(list.next()){
				System.out.println("\t" + list.getString("widget_name") + "\t" + list.getString("tc_id"));
				count++;
			}
			Test.printS();
			System.out.println("Succeed..." + count + " rows.");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	
	public static void listTCR(){
//		Connection conn = getConnection();	
		PreparedStatement pstmt = null;
		try {
			System.out.println("Try to list test case ");
			printS();	
			String sql = "select task_name,tc_id,test_result,tester from tcrun";
			pstmt = _conn.prepareStatement( sql );
			ResultSet list = pstmt.executeQuery();
			int count = 0;
			while(list.next()){
				System.out.println(list.getString("task_name").trim() + "\t\t" + list.getString("tc_id").trim() + "\t" + list.getString("test_result").trim() + "\t" + list.getString("tester").trim());
				count++;
			}
			printS();	
			System.out.println("Succeed..." + count + " rows.");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	
	public static void listTCR(String taskName){
//		Connection conn = getConnection();	
		PreparedStatement pstmt = null;
		try {
			System.out.println("Try to list test case ");
			printS();	
			String sql = "select test_result from tcrun where task_name='" + taskName + "'";
			pstmt = _conn.prepareStatement( sql );
			ResultSet list = pstmt.executeQuery();
			int count = 0;
			while(list.next()){
				System.out.println(list.getString("test_result"));
				count++;
			}
			printS();	
			System.out.println("Succeed..." + count + " rows.");
			pstmt.close();                    
//			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Fail");
			e.printStackTrace();
			try {
				pstmt.close();
//				conn.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
	} 

	public static void printS(){
		System.out.println("=============================================================================");
	}
	public static String getInputMessage(String prompt){
		if(!prompt.equals("") && !prompt.equalsIgnoreCase(PROMPT)){
	        System.out.print(prompt+"=");			
		}
        byte buffer[]=new byte[1024];
        int count = 0;
        try{
            count=System.in.read(buffer);
        }catch(Exception e){
        	
        }
        if(count<=2){
        	return "";
        }
        char[] ch=new char[count-2];

        for(int i=0;i<count-2;i++)
            ch[i]=(char)buffer[i];
        String str=new String(ch);
        return str;
    }




}
